package trees;

public class SerializeTree {
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        serialize(root, sb);

        return sb.toString();
    }

    public void serialize(TreeNode root, StringBuilder sb) {
        if (root == null) {
            sb.append("null").append(",");
            return;
        }

        sb.append(root.val).append(",");
        serialize(root.left, sb);
        serialize(root.right, sb);
    }

    public static void main(String[] args) {
        SerializeTree tree = new SerializeTree();
        TreeNode treeNode = TreeNode.buildTree(1);
        String result = tree.serialize(treeNode);
        System.out.println(result);
    }
}
